//
//  LandingViewController.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/5/27.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "LandingViewController.h"
#import "DSTextField.h"
@interface LandingViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *BJImageView;
// 登录框距离控制器View左边的间距
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *LoginViewLeftMargin;
- (IBAction)LoginButtonAction:(id)sender;
- (IBAction)ZCButtonAction:(id)sender;
- (IBAction)WJButtonAction:(id)sender;
@property (strong, nonatomic) IBOutlet DSTextField *PhoneText;
@property (strong, nonatomic) IBOutlet DSTextField *MMText;
@property (strong, nonatomic) IBOutlet DSTextField *ZCPhoneText;
@property (strong, nonatomic) IBOutlet DSTextField *ZCMMText;
- (IBAction)WBButtonAction:(id)sender;



@end

@implementation LandingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view insertSubview:self.BJImageView atIndex:0];
    self.PhoneText.keyboardType = UIKeyboardTypeNumberPad;
    self.ZCPhoneText.keyboardType = UIKeyboardTypeNumberPad;
    self.BJImageView.userInteractionEnabled = YES;
    self.PhoneText.delegate = self;
    self.MMText.delegate = self;
    self.ZCPhoneText.delegate = self;
    self.ZCMMText.delegate = self;
    
}
// 登录页面右上角按钮点击方法(注册账号 或 已有账号?)
- (IBAction)ZCorYY:(UIButton *)button {
    if (self.LoginViewLeftMargin.constant == 0) {// 显示注册界面
        self.LoginViewLeftMargin.constant = - self.view.frame.size.width;
        [button setTitle:@"已有账号?" forState:UIControlStateNormal];
        
    } else{// 显示登录页面
        self.LoginViewLeftMargin.constant = 0;
        [button setTitle:@"注册账号" forState:UIControlStateNormal];
        
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
        
    }];
}


// 让当前控制器的状态栏是白色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;

}
// 叉叉按钮
- (IBAction)XButtonAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
// 登录按钮点击方法
- (IBAction)LoginButtonAction:(id)sender {
    DSLog(@"登录按钮点击方法");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isUser = [[defaults objectForKey:self.PhoneText.text]isEqualToString:self.MMText.text];
    
    if (isUser == 1) {
        [defaults setObject:@"1" forKey:@"isLogin"];
        [defaults setObject:self.PhoneText.text forKey:@"userName"];
        [defaults setObject:self.MMText.text forKey:@"pwd"];
        [defaults synchronize];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
    }else{
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"用户名或者密码错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alertController animated:YES completion:^{
            
            
        }];
        
        
    }
}
// 注册按钮点击方法
- (IBAction)ZCButtonAction:(id)sender {
    DSLog(@"注册按钮点击方法");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (self.ZCPhoneText.text.length != 11) {
        UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"手机号输入错误" message:@"手机号为11位数" preferredStyle:UIAlertControllerStyleAlert];
        [alert1 addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alert1 animated:YES completion:^{
            
            
        }];
    
    }else if ((self.ZCPhoneText.text.length == 11) && (self.ZCMMText.text.length >= 6)){
        [defaults setObject:self.ZCMMText.text forKey:self.ZCPhoneText.text];
        [defaults synchronize];
        
        UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert2 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alert2 animated:YES completion:^{
            
            
        }];
        
    }else if (self.ZCMMText.text.length < 6){
        UIAlertController *alert3 = [UIAlertController alertControllerWithTitle:@"密码不能少于6位" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert3 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alert3 animated:YES completion:^{
            
            
        }];
    
    }
}
// 触摸手势点击屏幕 键盘回收
-(void)touchesEnded:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{

    [self.PhoneText resignFirstResponder];
    [self.MMText resignFirstResponder];
    [self.ZCPhoneText resignFirstResponder];
    [self.ZCMMText resignFirstResponder];
}
// 忘记密码?按钮点击方法
- (IBAction)WJButtonAction:(id)sender {
    DSLog(@"忘记密码?按钮点击方法");
}
// 微博按钮点击方法
- (IBAction)WBButtonAction:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //登录第二步
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {//登录成功
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
//            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [defaults setObject:snsAccount.userName forKey:@"userName"];
            [defaults setObject:snsAccount.iconURL forKey:@"headerUrl"];
            
            
            //登录第三步 ：在登录成功后 获取用户信息
            
            //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
            [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
                
                [defaults setObject:@"1" forKey:@"isLogin"];
                [self dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
            }];
            
            
            
        }});

}

@end
